//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.itheima.controller;

import com.itheima.pojo.Category;
import com.itheima.pojo.Result;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping({"/category"})
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    public CategoryController() {
    }

    @PostMapping
    public Result add(@RequestBody @Validated({Category.Add.class}) Category category) {
        this.categoryService.add(category);
        return Result.success();
    }

    @GetMapping
    public Result<List<Category>> list() {
        List<Category> cs = this.categoryService.list();
        return Result.success(cs);
    }

    @GetMapping({"/detail"})
    public Result<Category> detail(Integer id) {
        Category c = this.categoryService.finById(id);
        return Result.success(c);
    }

    @PutMapping
    public Result update(@RequestBody @Validated({Category.Update.class}) Category category) {
        this.categoryService.update(category);
        return Result.success();
    }
    @DeleteMapping
    public Result delete(Integer id) {
        this.categoryService.deleteById(id);
        return Result.success();
    }
}
